# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 9, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# DATAFRAME: REPUBLICANS
attach(our.data)
gop.data = data.frame(matchedGOP_govhealth, matchedGOP_govjobs, 
                      matchedGOP_govspending, year, VCF0009z, parties3)
gop.data = subset(gop.data, parties3=="Republican")
detach(our.data)

# DATAFRAME: DEMOCRATS
attach(our.data)
dems.data = data.frame(matchedDEM_govhealth, matchedDEM_govjobs, 
                       matchedDEM_govspending, year, VCF0009z, parties3)
dems.data = subset(dems.data, parties3=="Democrat")
detach(our.data)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# DESIGNS
gop.design = svydesign(ids=~1, weight=~gop.data$VCF0009z,
                       data = gop.data)
dems.design = svydesign(ids=~1, weight=~dems.data$VCF0009z,
                        data = dems.data)

# FOR LOOP
our.means.GOP = list(list())
our.props.GOP = list(list())
our.means.DEM = list(list())
our.props.DEM = list(list())
for(i in 1:3){
  # REPUBLICANS
  our.means.GOP[[i]] = svytable(~ gop.data[, i] + year, 
                                design = gop.design)
  our.props.GOP[[i]] = prop.table(our.means.GOP[[i]], 2)
  our.props.GOP[[i]]
  our.props.GOP[[i]] = as.data.frame(our.props.GOP[[i]])
  our.props.GOP[[i]] = subset(our.props.GOP[[i]], our.props.GOP[[i]][, 1]==1 & Freq!=0)
  names(our.props.GOP[[i]])[1] = "outcome"
  # DEMOCRATS 
  our.means.DEM[[i]] = svytable(~ dems.data[, i] + year, 
                                design = dems.design)
  our.props.DEM[[i]] = prop.table(our.means.DEM[[i]], 2)
  our.props.DEM[[i]]
  our.props.DEM[[i]] = as.data.frame(our.props.DEM[[i]])
  our.props.DEM[[i]] = subset(our.props.DEM[[i]], our.props.DEM[[i]][, 1]==1 & Freq!=0)
  names(our.props.DEM[[i]])[1] = "outcome"
}
our.means.DEM[[1]]

# PLOTTING DATA
newer.data = rbind(our.props.GOP[[1]], 
                   our.props.GOP[[2]], 
                   our.props.GOP[[3]],
                   our.props.DEM[[1]], 
                   our.props.DEM[[2]], 
                   our.props.DEM[[3]])
newer.data = newer.data[-c(1)]
newer.data$parties = c(rep("Republicans", 49),
                       rep("Democrats", 49))
newer.data$issue = rep(c(rep("Government health \ninsurance", length(our.props.GOP[[1]]$Freq)),
                         rep("Government \nguaranteed jobs", length(our.props.GOP[[2]]$Freq)),
                         rep("Government services \nand spending", length(our.props.GOP[[3]]$Freq))), 2)
newer.data$year = as.numeric(as.character(newer.data$year))
newer.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# FIGURE 9: PARTY COHESION ON SOCIAL WELFARE ISSUES: MATCHED AND ISSUE-SORTED
# PARTISANS IN THE PARTY COALITIONS, 1972-2020. 
our.plot = ggplot(newer.data,
                  aes(year, Freq, group = parties, 
                      linetype =  parties)) +
  geom_line(aes(linetype = parties), linewidth = 1) +
  labs(linetype = "Party ID") +  
  xlab("") +
  ylab("Proportion\n") + 
  theme_minimal() +
  # ggtitle("Party Cohesion on Social Welfare: Matched and Issue-Sorted \nPartisans in the Party Coalitions, 1972-2020", 
  #        subtitle = "Data: ANES cumulative file. Estimates indicate the proportion of Republican or \nDemocratic identifiers that matched their party's ideological orientation \nand shared their party's position.") + 
  # scale_color_grey(start = 0.8, end = 0.2) +
  facet_wrap(~issue, ncol = 1, scales = "free_x") +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9)) +
  theme(panel.spacing = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure9.pdf", width = 6.6, height = 9)
